From cbf26fbd02f365c0dfe8b210ffd8fe4c18a16e06 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Wed, 4 Apr 2018 12:01:17 +0200 Subject: [PATCH] widget: Add gtk_widget_compute_bounds() The first in a set of functions intended to query widget coordinates from another widget's coordinate system. --- docs/reference/gtk/gtk4-sections.txt | 1 + gtk/gtkwidget.c | 48 ++++++++++++++++++++++++++++ gtk/gtkwidget.h | 5 +++ 3 files changed, 54 insertions(+) diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index 5417c28c78..6c5b389736 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -4327,6 +4327,7 @@ gtk_widget_get_allocated_baseline gtk_widget_get_allocated_size gtk_widget_get_width gtk_widget_get_height +gtk_widget_compute_bounds gtk_widget_contains gtk_widget_pick gtk_widget_get_can_default diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c index d4c42d87ab..9bda1b729d 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -11574,6 +11574,54 @@ gtk_widget_get_own_allocation (GtkWidget *widget, allocation->height = priv->allocation.height -margin.top - margin.bottom; } +/** + * gtk_widget_compute_bounds: + * @widget: the #GtkWidget to query + * @target: the #GtkWidget + * @bounds: (out caller-allocates): the rectangle taking the bounds + * + * Computes the bounds for @widget in the coordinate space of @target. + * FIXME: Explain what "bounds" are. + * + * If the operation is successful, %TRUE is returned. If @widget has no + * bounds or the bounds cannot be expressed in @target's coordinate space + * (for example if both widgets are in different windows), %FALSE is + * returned and @bounds is set to the zero rectangle. + * + * It is valid for @widget and @target to be the same widget. + * + * Returns: %TRUE if the bonuds could be computed + **/ +gboolean +gtk_widget_compute_bounds (GtkWidget *widget, + GtkWidget *target, + graphene_rect_t *out_bounds) +{ + GtkAllocation alloc; + + g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE); + g_return_val_if_fail (GTK_IS_WIDGET (target), FALSE); + g_return_val_if_fail (out_bounds != NULL, FALSE); + + gtk_widget_get_own_allocation (widget, &alloc); + if (!gtk_widget_translate_coordinates (widget, + target, + alloc.x, alloc.y, + &alloc.x, &alloc.y)) + { + graphene_rect_init_from_rect (out_bounds, graphene_rect_zero ()); + return FALSE; + } + + graphene_rect_init (out_bounds, + alloc.x, + alloc.y, + alloc.width, + alloc.height); + + return TRUE; +} + /** * gtk_widget_get_allocated_width: * @widget: the widget to query diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h index 2892bc8eaa..22f2daf27c 100644 --- a/gtk/gtkwidget.h +++ b/gtk/gtkwidget.h @@ -623,6 +623,11 @@ GDK_AVAILABLE_IN_ALL void gtk_widget_get_allocation (GtkWidget *widget, GtkAllocation *allocation); GDK_AVAILABLE_IN_ALL +gboolean gtk_widget_compute_bounds (GtkWidget *widget, + GtkWidget *other, + graphene_rect_t *out_bounds); + +GDK_AVAILABLE_IN_ALL int gtk_widget_get_width (GtkWidget *widget); GDK_AVAILABLE_IN_ALL int gtk_widget_get_height (GtkWidget *widget); -- 2.30.2